/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-07
 * Time: 7:59
 */


class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    public int dfs(TreeNode root, int presum) {
        presum = presum * 10 + root.val;
        if(root.left == null && root.right == null) {
            return presum;
        }
        int ret = 0;
        if(root.left != null) {
            ret += dfs(root.left,presum);
        }
        if(root.right != null) {
            ret += dfs(root.right,presum);
        }

        return ret;
    }
}
